//
//  PileUpLayout.swift
//  CTCollectionViewDemo
//
//  Created by GodCoder on 2019/8/8.
//  Copyright © 2019 CTCollectionView. All rights reserved.
//

import UIKit

class PileUpLayout: UICollectionViewFlowLayout {

    override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
        
        let itemsLayouts = super.layoutAttributesForElements(in: rect)
        for layout in itemsLayouts! {
            let scale = CGFloat((10 - layout.indexPath.row)) * 0.1
            
            layout.center = CGPoint(x: layout.center.x, y: 130 + (100 - scale * 100))
            layout.transform = CGAffineTransform(scaleX: scale, y: scale)
            /// 设置item的顺序 第一个在最前面
            layout.zIndex = layout.indexPath.count - layout.indexPath.item;
        }
        
        return itemsLayouts
    }
    
    override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
        let itemLayout = super.layoutAttributesForItem(at: indexPath)
        // 重写
        let scale: CGFloat = CGFloat((indexPath.row + 1)/(indexPath.row + 2))
        
        itemLayout?.transform = CGAffineTransform(scaleX: scale, y: scale)
        itemLayout?.bounds = itemLayout!.bounds
        return itemLayout
    }
    
    override func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) -> Bool {
        return true
    }
}
